package cn.edu.sgu.www.mhxysy.service.children.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.children.ChildrenDTO;
import cn.edu.sgu.www.mhxysy.entity.children.*;
import cn.edu.sgu.www.mhxysy.factory.impl.ChildrenAttributeFactory;
import cn.edu.sgu.www.mhxysy.factory.impl.ChildrenJiadianFactory;
import cn.edu.sgu.www.mhxysy.factory.impl.ChildrenZizhiFactory;
import cn.edu.sgu.www.mhxysy.mapper.children.*;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.children.ChildrenPager;
import cn.edu.sgu.www.mhxysy.service.children.ChildrenService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChildrenServiceImpl implements ChildrenService {

    private final ChildrenMapper childrenMapper;
    private final ChildrenSkillMapper childrenSkillMapper;
    private final ChildrenZizhiMapper childrenZizhiMapper;
    private final ChildrenJiadianMapper childrenJiadianMapper;
    private final ChildrenAttributeMapper childrenAttributeMapper;
    private final ChildrenSkillCategoryMapper childrenSkillCategoryMapper;

    @Autowired
    public ChildrenServiceImpl(
            ChildrenMapper childrenMapper,
            ChildrenSkillMapper childrenSkillMapper,
            ChildrenZizhiMapper childrenZizhiMapper,
            ChildrenJiadianMapper childrenJiadianMapper,
            ChildrenAttributeMapper childrenAttributeMapper,
            ChildrenSkillCategoryMapper childrenSkillCategoryMapper) {
        this.childrenMapper = childrenMapper;
        this.childrenSkillMapper = childrenSkillMapper;
        this.childrenZizhiMapper = childrenZizhiMapper;
        this.childrenJiadianMapper = childrenJiadianMapper;
        this.childrenAttributeMapper = childrenAttributeMapper;
        this.childrenSkillCategoryMapper = childrenSkillCategoryMapper;
    }

    @Override
    public void init() {
        // 查询全部子女
        List<Children> list = childrenMapper.selectList(null);

        for (Children children : list) {
            Integer schoolId = children.getSchoolId();

            // 查询子女门派技能类型
            QueryWrapper<ChildrenSkillCategory> queryWrapper = new QueryWrapper<>();

            queryWrapper.eq("type", 4);
            queryWrapper.eq("school_id", schoolId);

            List<ChildrenSkillCategory> categories = childrenSkillCategoryMapper.selectList(queryWrapper);

            // 初始化子女技能
            for (int i = 0; i < categories.size(); i++) {
                ChildrenSkillCategory childrenSkillCategory = categories.get(i);

                ChildrenSkill childrenSkill = new ChildrenSkill();

                childrenSkill.setId(childrenSkill.uuid() + (i + 1));
                childrenSkill.setGrade(children.getGrade());
                childrenSkill.setChildrenId(children.getId());
                childrenSkill.setSkillId(childrenSkillCategory.getId());

                childrenSkillMapper.insert(childrenSkill);
            }
        }
    }

    @Override
    public void insert(ChildrenDTO childrenDTO) {
        // 1、添加子女
        Children children = toEntity(childrenDTO);

        children.setId(children.uuid());

        childrenMapper.insert(children);

        // 得到子女ID
        String childrenId = children.getId();

        // 2、添加子女技能
        QueryWrapper<ChildrenSkillCategory> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("type", 4);
        queryWrapper.eq("school_id", childrenDTO.getSchoolId());

        List<ChildrenSkillCategory> list = childrenSkillCategoryMapper.selectList(queryWrapper);

        for (int i = 0; i < list.size(); i++) {
            ChildrenSkill childrenSkill = new ChildrenSkill();

            childrenSkill.setId(childrenSkill.uuid() + (i + 1));
            childrenSkill.setGrade(childrenDTO.getGrade());
            childrenSkill.setChildrenId(childrenId);
            childrenSkill.setSkillId(list.get(i).getId());

            childrenSkillMapper.insert(childrenSkill);
        }

        // 3、添加子女资质
        ChildrenZizhiFactory childrenZizhiFactory = new ChildrenZizhiFactory();
        ChildrenZizhi childrenZizhi = childrenZizhiFactory.produce();

        childrenZizhi.setId(childrenZizhi.uuid());
        childrenZizhi.setChildrenId(childrenId);

        childrenZizhiMapper.insert(childrenZizhi);

        // 4、添加子女加点
        ChildrenJiadianFactory childrenJiadianFactory = new ChildrenJiadianFactory();
        ChildrenJiadian childrenJiadian = childrenJiadianFactory.produce();

        childrenJiadian.setId(childrenJiadian.uuid());
        childrenJiadian.setChildrenId(childrenId);

        childrenJiadianMapper.insert(childrenJiadian);

        // 5、添加子女属性
        ChildrenAttributeFactory childrenAttributeFactory = new ChildrenAttributeFactory();
        ChildrenAttribute childrenAttribute = childrenAttributeFactory.produce();

        childrenAttribute.setId(childrenAttribute.uuid());
        childrenAttribute.setJiadianId(childrenJiadian.getId());
        childrenAttribute.setChildrenId(childrenId);

        childrenAttributeMapper.insert(childrenAttribute);
    }

    @Override
    public void deleteById(String id) {
        // 1、删除子女
        childrenMapper.deleteById(id);

        // 构建删除条件
        Map<String, Object> columnMap = new HashMap<>();

        columnMap.put("children_id", id);

        // 2、删除子女技能
        childrenSkillMapper.deleteByMap(columnMap);

        // 3、删除子女加点
        childrenJiadianMapper.deleteByMap(columnMap);

        // 4、删除子女属性
        childrenAttributeMapper.deleteByMap(columnMap);

        // 5、删除子女资质
        childrenZizhiMapper.deleteByMap(columnMap);
    }

    @Override
    public void updateById(ChildrenDTO childrenDTO) {
        ObjectUtils.requireNonNull(childrenDTO.getId(), CustomMessages.ID_NOT_NULL);

        Children children = toEntity(childrenDTO);

        childrenMapper.updateById(children);
    }

    @Override
    public List<Children> selectAll() {
        return childrenMapper.selectList(null);
    }

    @Override
    public Children selectById(String id) {
        return childrenMapper.selectById(id);
    }

    @Override
    public Page<Children> selectByPage(ChildrenPager pager) {
        QueryWrapper<Children> queryWrapper = new QueryWrapper<>();
        Page<Children> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getSchoolId() != null,
                "school_id", pager.getSchoolId()
        );
        queryWrapper.eq(
                pager.getCategoryId() != null,
                "category_id", pager.getCategoryId()
        );
        queryWrapper.eq(
                StringUtils.isNotEmpty(pager.getRoleId()),
                "role_id", pager.getRoleId()
        );

        return childrenMapper.selectPage(page, queryWrapper);
    }

    private Children toEntity(ChildrenDTO childrenDTO) {
        Children children = new Children();

        children.setId(childrenDTO.getId());
        children.setName(childrenDTO.getName());
        children.setGrade(childrenDTO.getGrade());
        children.setRoleId(childrenDTO.getRoleId());
        children.setSchoolId(childrenDTO.getSchoolId());
        children.setCategoryId(childrenDTO.getCategoryId());
        children.setHealthiness(childrenDTO.getHealthiness());
        children.setImproveTimes(childrenDTO.getImproveTimes());

        return children;
    }

}